package com.wxdc.nmgyt.nmgytwydc.service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;

import com.syd.oden.circleprogressdialog.core.CircleProgressDialog;
import com.wxdc.nmgyt.nmgytwydc.app.MyApp;
import com.wxdc.nmgyt.nmgytwydc.common.CustomDialog;
import com.wxdc.nmgyt.nmgytwydc.common.MyCircleProgressDialog;
import com.wxdc.nmgyt.nmgytwydc.utils.LocationServiceUtil;

/**
 * Created by Administrator on 2018/5/4.
 */

public class MyLocationService extends Service {

    public static final String TAG = "MyLocationService";
    public static final int GETLOCATION_SUCCESS = 0x11000;
    private MyBinder mBinder = new MyBinder();
    private Thread mThread;
    private volatile Location mLocation;
    private boolean flag = true;
    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG, "onCreate: ----------------");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG, "onStartCommand: ---------------------");
        return super.onStartCommand(intent, flags, startId);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.i(TAG, "onBind: ----------------------");
        return mBinder;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.i(TAG, "onUnbind: -------------------------");
        return super.onUnbind(intent);
    }

    @Override
    public void onDestroy() {
        Log.i(TAG, "onDestroy: -------------------");
        super.onDestroy();
        if (mThread != null && mThread.isAlive()) {
            mThread.interrupt();
            mThread = null;
        }
        flag = false;
    }
    private Object objLock = new Object();
    public class  MyBinder extends Binder{

        private LocationServiceUtil l = LocationServiceUtil.getInstance(MyApp.getInstance().getApplicationContext());
       /* isAlive() 测试线程是否处于活动状态。
        isInterrupted()测试线程是否已经中断
        isDaemon()方法用来测试线程是否为守护线程*/

        public Location getLocation(){
            flag = true;
            mLocation = l.getLocation();
            if (mLocation == null) {
                synchronized (objLock) {
                    if (mLocation == null) {
                        if (mThread == null) {
                            mThread = new Thread(new Runnable() {
                                @Override
                                public void run() {
                                    while (flag) {
                                        mLocation = l.getLocation();
                                        if (mLocation != null) {
                                            flag = false;
                                        }
                                        //Log.i(TAG, "run: ------------" + mLocation);
                                    }
                                }
                            });
                        }
                        if (!mThread.isAlive()){
                            mThread.start();
                        }
                    }
                    return mLocation;
                }
            } else {
                return mLocation;
            }
        }
    }
}
